package hu.myai.gui.widgets.graph;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;

public class Grid2 implements IGraphGrid {
    private ScalarGraph graph;

    // grid
    private Color gridColor;
    private boolean gridLabelsVisible = true;
    private Color gridLabelForeground;
    private Color gridLabelBackground;

    @Override
    public void paintGrid(PaintEvent e) {
        int n = graph.getScalar().getDepth();
        double min = graph.getMin();
        double max = graph.getMax();
        int w = graph.getSize().x;
        int h = graph.getSize().y;

        // draw vertical grid
        int pn = 0;
        int dn = n / 5;
        e.gc.setForeground(gridColor);
        while (true) {
            int px = pn * (w - 1) / (n - 1);
            if (px >= w)
                break;
            e.gc.drawLine(px, 0, px, h - 1);
            pn += dn;
        }

        // draw horizontal grid
        double log = Math.log10(max - min);
        int ex = (int) log;
        if (log < 0)
            ex -= 1;
        double step = Math.pow(10.0, ex);
        int k = Math.abs(((int) (max / step)) - ((int) (min / step)));
        if ((max * min) < 0)
            k += 1;
        if (k > 5)
            step *= 2;
        if (k < 3)
            step /= 2;
        double init = step * ((int) (min / step));
        e.gc.setBackground(gridLabelBackground);
        while (true) {
            if (init > max)
                break;
            int py = (int) (((h - 1) * (max - init)) / (max - min));
            e.gc.setForeground(gridColor);
            e.gc.drawLine(0, py, w - 1, py);

            // draw grid label
            if (gridLabelsVisible) {
                String s = String.format("%+3.3f", init);
                Point size = e.gc.textExtent(s);
                e.gc.setForeground(gridLabelForeground);
                e.gc.fillRectangle(0, py - size.y / 2 - 2, size.x + 4, size.y + 3);
                e.gc.drawText(s, 2, py - size.y / 2 - 1);
            }
            init += step;
        }
    }

    public Color getGridColor() {
        return gridColor;
    }

    public void setGridColor(Color gridColor) {
        this.gridColor = gridColor;
    }

    public boolean isGridLabelsVisible() {
        return gridLabelsVisible;
    }

    public void setGridLabelsVisible(boolean gridLabelsVisible) {
        this.gridLabelsVisible = gridLabelsVisible;
    }

    public Color getGridLabelForeground() {
        return gridLabelForeground;
    }

    public void setGridLabelForeground(Color gridLabelForeground) {
        this.gridLabelForeground = gridLabelForeground;
    }

    public Color getGridLabelBackground() {
        return gridLabelBackground;
    }

    public void setGridLabelBackground(Color gridLabelBackground) {
        this.gridLabelBackground = gridLabelBackground;
    }

    public ScalarGraph getGraph() {
        return graph;
    }

    public Grid2(ScalarGraph graph) {
        this.graph = graph;
    }
}
